package com.miaostars.apakey;

import com.miaostars.apakey.config.ConfigManager;
import com.miaostars.apakey.events.*;
import com.miaostars.apakey.utils.AlertManager;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;

public class Apakey extends JavaPlugin {
    
    private static Apakey instance;
    private ConfigManager configManager;
    private AlertManager alertManager;
    
    @Override
    public void onEnable() {
        instance = this;
        
        // Initialize managers
        configManager = new ConfigManager(this);
        alertManager = new AlertManager(this);
        
        // Load configuration
        configManager.loadConfig();
        
        // Register events
        Bukkit.getPluginManager().registerEvents(new MovementListener(this), this);
        Bukkit.getPluginManager().registerEvents(new CombatListener(this), this);
        Bukkit.getPluginManager().registerEvents(new InventoryListener(this), this);
        Bukkit.getPluginManager().registerEvents(new BlockListener(this), this);
        
        getLogger().info("Apakey has been enabled!");
    }
    
    @Override
    public void onEnable() {
       instance = this;
    
        // Initialize managers
       configManager = new ConfigManager(this);
       alertManager = new AlertManager(this);
    
       // Load configuration
       configManager.loadConfig();
    
       // Register events
       Bukkit.getPluginManager().registerEvents(new MovementListener(this), this);
       Bukkit.getPluginManager().registerEvents(new CombatListener(this), this);
       Bukkit.getPluginManager().registerEvents(new InventoryListener(this), this);
       Bukkit.getPluginManager().registerEvents(new BlockListener(this), this);
       Bukkit.getPluginManager().registerEvents(new PlayerJoinListener(this), this); // 新增这行
    
       getLogger().info("Apakey has been enabled!");
    }

    
    @Override
    public void onDisable() {
        getLogger().info("Apakey has been disabled!");
    }
    
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("apakey")) {
            if (args.length == 0) {
                sender.sendMessage("§cUsage: /apakey [reload|alerts]");
                return true;
            }
            
            switch (args[0].toLowerCase()) {
                case "reload":
                    if (!sender.hasPermission("apakey.admin")) {
                        sender.sendMessage("§cYou don't have permission to do that!");
                        return true;
                    }
                    configManager.reloadConfig();
                    sender.sendMessage("§aApakey configuration reloaded!");
                    break;
                    
                case "alerts":
                    if (!sender.hasPermission("apakey.alerts")) {
                        sender.sendMessage("§cYou don't have permission to do that!");
                        return true;
                    }
                    alertManager.toggleAlerts(sender);
                    break;
                    
                default:
                    sender.sendMessage("§cUnknown subcommand. Usage: /apakey [reload|alerts]");
            }
            return true;
        }
        return false;
    }
    
    public static Apakey getInstance() {
        return instance;
    }
    
    public ConfigManager getConfigManager() {
        return configManager;
    }
    
    public AlertManager getAlertManager() {
        return alertManager;
    }
}